#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2005;

int t, k;
int c[N][N];
int sum[N][N];
void init() {
  rep(i, 0, 2000) c[i][0] = 1;
  rep(i, 1, 2000) rep(j, 0, i) c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % k;
  rep(i, 0, 2000) rep(j, i + 1, 2000) c[i][j] = 1;
  rep(i, 0, 2000) sum[0][i] = sum[i][0] = 0;
  rep(i, 1, 2000) rep(j, 1, 2000) {
    sum[i][j] = sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1] +
                (c[i][j] == 0 ? 1 : 0);
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> t >> k;
  init();
  while (t--) {
    int n, m;
    cin >> n >> m;
    cout << sum[n][m] << endl;
  }
  return 0;
}